//
//  TrainView.m
//  AoJi
//
//  Created by IMacLife on 15/2/3.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//
//我的培训

#import "TrainView.h"

@implementation TrainView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadView];
        [self loadData];
    }
    return self;
}

//加载view
-(void)loadView
{
    
    NSArray *segmentArray = @[@"所报课程",@"测试信息"];
    segment = [[UISegmentedControl alloc]initWithItems:segmentArray];
    segment.frame = CGRectMake(10, 5, Mainwidth-20, 35);
    segment.selectedSegmentIndex=0;  //设置默认选择项索引
    segment.tintColor=[UIColor redColor]; //选中颜色
    [segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:segment];
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(5, 50, Mainwidth-10, MainHeight) style:UITableViewStylePlain];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.backgroundColor=[UIColor clearColor];
    _tableview.separatorStyle=UITableViewCellAccessoryNone;
    [self addSubview:_tableview];
    
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor=[Color colorFromHexRGB:@"e4e4e4"];
    _tableview.tableFooterView=footView;
}

#pragma mark -----------------Action
-(void)segmentClick:(UISegmentedControl *)sender
{
    if (segment.selectedSegmentIndex==0) {
        [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:nil WithApi:@"me/train" WhenSuccess:^(NSMutableDictionary *response) {
            NSLog(@"%@",response);
            dataArray = response;
            [_tableview reloadData];
        } WhenFailure:^(NSError *failure) {
            NSLog(@"%@",failure);
        }];
    }else{
        [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:nil WithApi:@"me/train/quiz" WhenSuccess:^(NSMutableDictionary *response) {
            NSLog(@"%@",response);
            dataArray = response;
            [_tableview reloadData];
        } WhenFailure:^(NSError *failure) {
            NSLog(@"%@",failure);
        }];
    }

}

-(void)loadData
{
    //http://cent.dsgrd.com/me/train
    //http://cent.dsgrd.com/me/train/progress?name=<course_name>
    //http://cent.dsgrd.com/me/train/quiz
    
        [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:nil WithApi:@"me/train" WhenSuccess:^(NSMutableDictionary *response) {
            NSLog(@"%@",response);
            dataArray = response;
            [_tableview reloadData];
        } WhenFailure:^(NSError *failure) {
            NSLog(@"%@",failure);
        }];
}

#pragma mark ----------------UITableViewDelegate
//一个分区显示多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


//UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, tableView.frame.size.width, 25)];
    name.text = dataArray[indexPath.row][@"课程名称"];
    name.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:name];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 26, tableView.frame.size.width, 1)];
    line.backgroundColor=[Color colorFromHexRGB:@"f0f0f0"];
    [cell.contentView addSubview:line];
    
    UILabel *baoming = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, tableView.frame.size.width/3, 25)];
    baoming.font = [UIFont systemFontOfSize:13];
    baoming.text=@"报名时间";
    baoming.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:baoming];
    
    UILabel *baomingTime = [[UILabel alloc]initWithFrame:CGRectMake(0, 55, tableView.frame.size.width/3, 25)];
    baomingTime.font = [UIFont systemFontOfSize:13];
    baomingTime.text=dataArray[indexPath.row][@"报名时间"];
    baomingTime.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:baomingTime];
    
    UILabel *first = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width/3, 30, tableView.frame.size.width/3, 25)];
    first.font = [UIFont systemFontOfSize:13];
    first.text=@"首次上课";
    first.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:first];
    
    UILabel *firstTime = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width/3, 55, tableView.frame.size.width/3, 25)];
    firstTime.font = [UIFont systemFontOfSize:13];
    firstTime.text=dataArray[indexPath.row][@"入班时间"];
    firstTime.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:firstTime];
    
    UILabel *endts = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width/3*2, 30, tableView.frame.size.width/3, 25)];
    endts.font = [UIFont systemFontOfSize:13];
    endts.text=@"结束时间";
    endts.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:endts];
    
    UILabel *endTime = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width/3*2, 55, tableView.frame.size.width/3, 25)];
    endTime.font = [UIFont systemFontOfSize:13];
    endTime.text=dataArray[indexPath.row][@"出班时间"];
    endTime.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:endTime];
    
    UIView *spacing = [[UIView alloc]initWithFrame:CGRectMake(0, 80, tableView.frame.size.width, 5)];
    spacing.backgroundColor=[Color colorFromHexRGB:@"f0f0f0"];
    [cell.contentView addSubview:spacing];
    
    return cell;
}


//cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}




@end
